package com.pp.ent.db;

import com.pp.ent.base.log.MLog;

/**
 * Created by dengqu on 2017/2/24.
 */

public abstract class DbCommand<T> {
    protected DbResult<T> result;
    protected DbResponseLister dbResponseLister;
    private String uri;


    protected DbCommand(String uri) {
        this(uri, null);
    }

    protected DbCommand(DbResponseLister dbResponseLister) {
        this(null, dbResponseLister);
    }

    protected DbCommand(String uri, DbResponseLister dbResponseLister) {
        this.uri = uri;
        this.dbResponseLister = dbResponseLister;
    }


    protected void realExecute() {
        this.result = new DbResult();
        try {
            this.result.data = execute();
        } catch (Exception e) {
            this.result.result = DbResult.ResultCode.Failed;
            MLog.error("db execute error", e);
        }
    }

    public abstract T execute()
            throws Exception;

    public DbResult getResult() {
        return this.result;
    }

    public String getUri() {
        return this.uri;
    }

    public DbResponseLister getDbResponseLister() {
        return dbResponseLister;
    }
}
